package org.example.flinksql.test.custom;

import com.google.gson.Gson;
import org.apache.flink.table.annotation.DataTypeHint;
import org.apache.flink.table.functions.ScalarFunction;
import org.example.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

public class TransShouldCapiValueFunc extends ScalarFunction { ;
    private Logger logger = LoggerFactory.getLogger(TransShouldCapiValueFunc.class);

    @DataTypeHint("DECIMAL(20, 6)")
    public BigDecimal eval(String str) {
        if (null == str) {
            return null;
        }

        try {
            List list = new Gson().fromJson(str, List.class);
            Map<String,String> map = (Map<String, String>) list.get(0);
            String shouldCapi = map.get("shoud_capi");
            Double aDouble = Utils.getDoubleFromStr(shouldCapi);
            BigDecimal bigDecimal = BigDecimal.valueOf(aDouble);
            return bigDecimal;
        } catch (Exception e) {
            logger.warn(String.format("%s :: %s :: [%s]", e.getMessage(), e.getCause(), str));
            return null;
        }
    }
}
